import { defineStore } from 'pinia'
import  { reqIndexCurrency } from '@/api/index'
import { ref } from 'vue'

// 用户相关数据
export const useUserStore = defineStore('user', () => {
 const token = ref('');
 const mail = ref('');
 const ouser_id = ref(''); // 运维id

 const setUserInfo = (data) => {
   if(data?.token) {
      token.value = data.token
      localStorage.setItem('token', data.token)
   } else {
      token.value = localStorage.getItem('token')
   }
   if(data?.mail) {
      mail.value = data.mail
      localStorage.setItem('mail', data.mail)
   } else {
      mail.value = localStorage.getItem('mail')
   }
   if(data?.ouser_id) {
      ouser_id.value = data.ouser_id
      localStorage.setItem('ouser_id', data.ouser_id)
   } else {
      ouser_id.value = localStorage.getItem('ouser_id')
   }
 }

 return {ouser_id,mail,setUserInfo}
});

// 下拉框选项相关数据
export const useSelectOptionsStore = defineStore('selectOptions', () => {
  const currencies = ref([]);

  const setCurrencies = async () => {
      const storageCurrencies = localStorage.getItem('currencies');
      if(storageCurrencies) {
         currencies.value = JSON.parse(storageCurrencies);
         return;
      }
      const res = await reqIndexCurrency();
      currencies.value = res.data.map((item) => ({
         value: item.currency,
         label: item.currency
      }));
      localStorage.setItem('currencies', JSON.stringify(currencies.value));
  }
  return { currencies, setCurrencies }
})